'''
Created on 18 dec. 2011

@author: Daan
'''
from Tkinter import Tk, Canvas

from Maps.BasicMap import BasicMap
from Tick import Tick
from Car import Car
from CarList import CarList

class Main(object):
    def __init__(self, width=800, height=600, cars=100, lanes=5):
        self.window = Tk()
        self.window.title("TrafficSim")
        self.window.protocol("WM_DELETE_WINDOW", self.Close)
        
        self.canvas = Canvas(self.window, width=width, height=height)
        self.canvas.pack()
        
        self.bmap = BasicMap(5, 5, 715, 415, lanes, self.canvas)
        
        self._cars = list(Car((i % lanes) + 1, self.bmap, maxSpeed=300) for i in range(cars))
        
#        self._cars = []
#        for i in range(cars) :
#            self._cars.append(Car((i % lanes) + 1, self.bmap, maxSpeed=300))
        
        self._keepRunning = True
        
    def Close(self):
        self._keepRunning = False
        
    def Open(self):
        tick = Tick()
        while self._keepRunning:
            tick.Update()
            for car in self._cars:
                car.Update(tick)
                self.window.update_idletasks()
            self.window.update()

class Test(object):
    def _linit__(self, i, n):
        self._l = i
        self._n = n
    
    def __str__(self):
        return "%s %s" % (self._n, self._l)
    
    def __repr__(self):
        return self.__str__()
    
    def getI(self):
        return self._l

if __name__ == '__main__':
    
    carList = CarList()
    
    for i in range(10):
        x = carList.AddFront(Test(i, "a"))
        carList.AddBefore(x, Test(10-i,"b"))
        
    carList.Print()
    
    for x in carList:
        xi = x.getI()
        print "%s x.getI(): %s" % (x, xi)
        if x.getI() < 10:
            carList.Remove(x)
    
    carList.Print()
    
#    Main(cars=6, lanes=3).Open()
